package cn.iocoder.yudao.module.lfd.dal.dataobject.appointment;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.lfd.controller.admin.appointment.vo.AppointmentEnum;
import cn.iocoder.yudao.module.lfd.controller.admin.appointment.vo.AppointmentUpdateReqVO;
import cn.iocoder.yudao.module.lfd.controller.admin.appointment.vo.PayTypeEnum;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 预约 DO
 *
 * @author 芋道源码
 */
@TableName("lfd_appointment")
@KeySequence("lfd_appointment_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库，可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AppointmentDO extends BaseDO {

    /**
     * id
     */
    @TableId
    private Long id;
    /**
     * openid
     */
    private String openid;
    /**
     * 流水号
     */
    private String flowNo;
    /**
     * 店铺id
     */
    private Long shopId;
    /**
     * 状态
     */
    private AppointmentEnum status;
    /**
     * 理发师id
     */
    private Long barberId;
    /**
     * 等待时间
     */
    private Long costTime;
    /**
     * 金额
     */
    private BigDecimal money;
    /**
     * 开始理发的时间，当前用户需要的时间-（现在-开始时间）
     */
    private LocalDateTime startHairTime;

    private LocalDate hairDate;

    private Double integral;

    private String number;

    private PayTypeEnum payType;

    private String items;

    public Integer toFen() {
        return this.getMoney().multiply(new BigDecimal("100")).intValue();
    }

    public void barbering() {
        this.status = AppointmentEnum.BARBERING;
        this.startHairTime = LocalDateTime.now();
    }

    public void cancel() {
        this.status = AppointmentEnum.CANCEL;
        this.startHairTime = LocalDateTime.now();
    }

    public void unpaid() {
        this.status = AppointmentEnum.UNPAID;
        this.startHairTime = LocalDateTime.now();
    }

    public void finish(PayTypeEnum payType, String flowNo) {
        this.status = AppointmentEnum.FINISH;
        this.payType = payType;
        this.flowNo = flowNo;
        this.startHairTime = LocalDateTime.now();
    }

    //当前预约已经理发的时间，从开始理发到现在
    private long hairTime() {
        if (this.getStartHairTime() == null) {
            return 0L;
        }
        long between = LocalDateTimeUtil.between(this.getStartHairTime(), LocalDateTime.now(), ChronoUnit.MINUTES);
        //小于0，理论上不可能
        if (between < 0) {
            return 0L;
        }
        return between;
    }

    public long remaiTime() {
        long remain = this.costTime - hairTime();
        return remain > 0 ? remain : 0;
    }

}
